/*-------------------------------------------------------------------------
 *
 * brindesc.c
 *	  BRIN 索引的 rmgr 描述例程
 *
 * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *	  src/backend/access/rmgrdesc/brindesc.c
 *
 *-------------------------------------------------------------------------
 */
#include "postgres.h"

#include "access/brin_xlog.h"

void brin_desc(StringInfo fc_buf, XLogReaderState *fc_record)
{
	char	   *fc_rec = XLogRecGetData(fc_record);
	uint8		fc_info = XLogRecGetInfo(fc_record) & ~XLR_INFO_MASK;

	fc_info &= XLOG_BRIN_OPMASK;
	if (fc_info == XLOG_BRIN_CREATE_INDEX)
	{
		xl_brin_createidx *fc_xlrec = (xl_brin_createidx *) fc_rec;

		appendStringInfo(fc_buf, "v%d pagesPerRange %u",
						 fc_xlrec->version, fc_xlrec->pagesPerRange);
	}
	else if (fc_info == XLOG_BRIN_INSERT)
	{
		xl_brin_insert *fc_xlrec = (xl_brin_insert *) fc_rec;

		appendStringInfo(fc_buf, "heapBlk %u pagesPerRange %u offnum %u",
						 fc_xlrec->heapBlk,
						 fc_xlrec->pagesPerRange,
						 fc_xlrec->offnum);
	}
	else if (fc_info == XLOG_BRIN_UPDATE)
	{
		xl_brin_update *fc_xlrec = (xl_brin_update *) fc_rec;

		appendStringInfo(fc_buf, "heapBlk %u pagesPerRange %u old offnum %u, new offnum %u",
						 fc_xlrec->insert.heapBlk,
						 fc_xlrec->insert.pagesPerRange,
						 fc_xlrec->oldOffnum,
						 fc_xlrec->insert.offnum);
	}
	else if (fc_info == XLOG_BRIN_SAMEPAGE_UPDATE)
	{
		xl_brin_samepage_update *fc_xlrec = (xl_brin_samepage_update *) fc_rec;

		appendStringInfo(fc_buf, "offnum %u", fc_xlrec->offnum);
	}
	else if (fc_info == XLOG_BRIN_REVMAP_EXTEND)
	{
		xl_brin_revmap_extend *fc_xlrec = (xl_brin_revmap_extend *) fc_rec;

		appendStringInfo(fc_buf, "targetBlk %u", fc_xlrec->targetBlk);
	}
	else if (fc_info == XLOG_BRIN_DESUMMARIZE)
	{
		xl_brin_desummarize *fc_xlrec = (xl_brin_desummarize *) fc_rec;

		appendStringInfo(fc_buf, "pagesPerRange %u, heapBlk %u, page offset %u",
						 fc_xlrec->pagesPerRange, fc_xlrec->heapBlk, fc_xlrec->regOffset);
	}
}

const char * brin_identify(uint8 fc_info)
{
	const char *fc_id = NULL;

	switch (fc_info & ~XLR_INFO_MASK)
	{
		case XLOG_BRIN_CREATE_INDEX:
			fc_id = "CREATE_INDEX";
			break;
		case XLOG_BRIN_INSERT:
			fc_id = "INSERT";
			break;
		case XLOG_BRIN_INSERT | XLOG_BRIN_INIT_PAGE:
			fc_id = "INSERT+INIT";
			break;
		case XLOG_BRIN_UPDATE:
			fc_id = "UPDATE";
			break;
		case XLOG_BRIN_UPDATE | XLOG_BRIN_INIT_PAGE:
			fc_id = "UPDATE+INIT";
			break;
		case XLOG_BRIN_SAMEPAGE_UPDATE:
			fc_id = "SAMEPAGE_UPDATE";
			break;
		case XLOG_BRIN_REVMAP_EXTEND:
			fc_id = "REVMAP_EXTEND";
			break;
		case XLOG_BRIN_DESUMMARIZE:
			fc_id = "DESUMMARIZE";
			break;
	}

	return fc_id;
}
